package best.driver.android.Activity.Order.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import best.driver.android.Activity.Order.OrderDetailActivity;
import best.driver.android.Activity.Order.OrderFeeActivity;
import best.driver.android.Activity.Order.OrderOperationActivity;
import best.driver.android.Beans.*;
import best.driver.android.BestApp;
import best.driver.android.R;
import best.driver.android.Utils.SizeUtils;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.Utils.Utils;
import best.driver.android.common.utils.StringUtils;
import best.driver.android.manager.OrderManager;
import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.List;

public class OrderItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private ArrayList<OrderListInfo.Records> mOrderItems = new ArrayList<OrderListInfo.Records>();

    public OrderItemAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderListInfo.Records> list) {
        mOrderItems.clear();
        mOrderItems.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mOrderItems.size();
    }

    @Override
    public OrderListInfo.Records getItem(int position) {
        return mOrderItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.item_order_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderListInfo.Records item = getItem(position);
                if(OrderManager.getInstance().isProcessing(item.status)) {
                    Intent intent = new Intent(mContext, OrderOperationActivity.class);
                    intent.putExtra("orderId", item.orderId);
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, OrderDetailActivity.class);
                    intent.putExtra("orderId", item.orderId);
                    mContext.startActivity(intent);
                }
            }
        });
        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        OrderListInfo.Records item = getItem(position);
       // if(OrderStatus.WAIT_START.equals(item.status)) {
            holder.mTvOrderTime.setText(TimeUtil.formatTime(item.bookTime));
       // } else {
       //     holder.mTvOrderTime.setText(TimeUtil.convertToMDHM(item.orderTime));
       // }
        List<OrderAddresses> addresses =  item.orderAddresses;
        if(addresses != null && addresses.size() > 0) {
            for(int i = 0; i < addresses.size(); i++) {
                OrderAddresses address = addresses.get(i);
                if(OrderStatus.SHIPPER_ADDRESS.equals(address.flag)) {
                    holder.mTvOrderStartAddress.setText(address.address);
                } else if (OrderStatus.RECEIVER_ADDRESS.equals(address.flag)) {
                    holder.mTvOrderDestination.setText(address.address);
                }
            }
        }
        holder.mTvOrderStatus.setText(getStatusDesc(item.status));
        if(OrderStatus.CANCELED.equals(item.status)) {
            holder.mTvOrderType.setBackgroundResource(R.drawable.circle_gray_border);
            holder.mTvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.ccc));
            holder.mIvPickup.setImageResource(R.drawable.icon_carry_d);
            holder.mIvUnload.setImageResource(R.drawable.icon_remove_d);
            holder.mTvOrderStartAddress.setTextColor(mContext.getResources().getColor(R.color.ccc));
            holder.mTvOrderDestination.setTextColor(mContext.getResources().getColor(R.color.ccc));

        } else {
            if(OrderStatus.REAL_TIME.equals(item.type)) {
                holder.mTvOrderType.setBackgroundResource(R.drawable.circle_red_border);
                holder.mTvOrderType.setText("实时");
            } else {
                holder.mTvOrderType.setBackgroundResource(R.drawable.circle_blue_border);
                holder.mTvOrderType.setText("预约");
            }
            holder.mTvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.best_red));
            holder.mIvPickup.setImageResource(R.drawable.icon_carry_n);
            holder.mIvUnload.setImageResource(R.drawable.icon_remove_n);
            holder.mTvOrderStartAddress.setTextColor(mContext.getResources().getColor(R.color.aaaa));
            holder.mTvOrderDestination.setTextColor(mContext.getResources().getColor(R.color.aaaa));
        }

        if(OrderStatus.RETURNING.equals(item.status) || OrderStatus.FINISHED.equals(item.status)) {
            holder.mTvPrePrice.setText(Util.getOnePointNumber(item.actualPrice) + "元");
        } else {
            holder.mTvPrePrice.setText("预估" + Util.getOnePointNumber(item.estimatePrice) + "元");
        }

        if(item.familiarPrice > 0.1) {
            holder.mTvPrePrice.setText("一口价" + Util.getOnePointNumber(item.familiarPrice) + "元");
        }

        if(OrderStatus.ONE_TO_ONE.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        } else if (OrderStatus.ONE_TO_N.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.VISIBLE);
        } else if (OrderStatus.N_TO_ONE.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.VISIBLE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        } else {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        }

        if(OrderStatus.CANCELED.equals(item.status)) {
            holder.mTvUnloadMulti.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.circle_gray_gray));
            holder.mTvUnloadMulti.setPadding((int)(8 * SizeUtils.getDensity(mContext)), 0, (int)(8 * SizeUtils.getDensity(mContext)), 0);
            holder.mTvOrderTime.setTextColor(mContext.getResources().getColor(R.color.ccc));
            holder.mTvUnloadMulti.setTextColor(mContext.getResources().getColor(R.color.ccc));
        } else {
            holder.mTvUnloadMulti.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.circle_gray_yellow));
            holder.mTvUnloadMulti.setPadding((int)(8 * SizeUtils.getDensity(mContext)), 0, (int)(8 * SizeUtils.getDensity(mContext)), 0);
            holder.mTvOrderTime.setTextColor(mContext.getResources().getColor(R.color.aaaa));
            holder.mTvUnloadMulti.setTextColor(mContext.getResources().getColor(R.color.best_yellow));
        }
    }

    static class ViewHolder {

        @InjectView(R.id.tv_order_type)
        TextView mTvOrderType;

        @InjectView(R.id.tv_order_time)
        TextView mTvOrderTime;

        @InjectView(R.id.tv_order_status)
        TextView mTvOrderStatus;

        @InjectView(R.id.tv_order_start_address)
        TextView mTvOrderStartAddress;

        @InjectView(R.id.tv_order_destination)
        TextView mTvOrderDestination;

        @InjectView(R.id.tv_pre_price)
        TextView mTvPrePrice;

        @InjectView(R.id.btn_operation)
        Button mBtnOrderOperation;

        @InjectView(R.id.tv_load_multi)
        TextView mTvLoadMulti;

        @InjectView(R.id.tv_unload_multi)
        TextView mTvUnloadMulti;

        @InjectView(R.id.iv_pick_up)
        ImageView mIvPickup;

        @InjectView(R.id.iv_unload)
        ImageView mIvUnload;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    public String getStatusDesc(String status) {
        if(OrderStatus.WAIT_START.equals(status)) {
            return "待出发";
        } else if (OrderStatus.STARTED.equals(status)) {
            return "已出发";
        } else if (OrderStatus.LOADING.equals(status)) {
            return "装货中";
        } else if (OrderStatus.IN_TRANSIT.equals(status)) {
            return "运输中";
        } else if (OrderStatus.UNLOADING.equals(status)) {
            return "卸货中";
        } else if (OrderStatus.RETURNING.equals(status)) {
            return "待送回单/回款";
        } else if (OrderStatus.FINISHED.equals(status)) {
            return "已完成";
        } else if (OrderStatus.CANCELED.equals(status)) {
            return "已取消";
        }
        return "";
    }
}
